<template>
  <van-nav-bar title="登录" left-text="返回" left-arrow @click-left="back()" />
  <div class="box">
    <van-field v-model="input" label="账号" placeholder="请输入用户名" />
    <van-field v-model="password" label="密码" placeholder="请输入密码" />
    <van-button type="primary" @click="btn()">登录</van-button>
  </div>
  <div class="txt" @click="this.$router.push('/reg')">没账号?,滚去注册</div>
</template>
<script>
import { loginreq } from '../../api/requies'

export default {
  methods: {
    back() {
      this.$router.back()
    },
    btn() {
      loginreq(this.input, this.password).then((r) => {
        console.log(r.data)
        if (r.data.code == 1) {
          localStorage.setItem('token', r.data.token)
          localStorage.setItem('id', r.data.user.id)
          this.$router.push('/')
        } else {
          alert(r.data.message)
        }
      })
    }
  },
  data() {
    return {
      input: '',
      password: ''
    }
  }
}
</script>
<style lang="scss">
.box {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.txt {
  color: red;
  font-size: 20px;
  margin-top: 30px;
  position: absolute;
  right: 10px;
}
</style>
